package Gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ConnectionClient;
/**
 * A class which represents the append tab.
 * @author ic08jh7
 *
 */
public class AppendRecordPane extends BasicPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3919207721354094364L;

	private JTextField[] fields;

	private static final int SSN = 0;

	private static final int NURSE = 1;

	private static final int DIVISION = 2;

	private static final int SYMPTOMES = 3;

	private static final int DIAGNOSIS = 4;

	private static final int FULL_NAME = 5;

	/**
	 * The total number of fields.
	 */
	private static final int NBR_FIELDS = 6;
	/**
	 * Creates a new append tab which communicates via the connection client.
	 * @param cc
	 * 			The connection client.
	 */
	public AppendRecordPane(ConnectionClient cc) {
		super(cc);
	}
	/**
	 * Creates the top panel.
	 */
	@Override
	public JComponent createTopPanel() {
		String[] texts = new String[NBR_FIELDS];
		texts[SSN] = "Social security number";
		texts[NURSE] = "Nurse";
		texts[DIVISION] = "Division";
		texts[SYMPTOMES] = "Symptomes";
		texts[DIAGNOSIS] = "Diagnosis";
		texts[FULL_NAME] = "Full name";

		fields = new JTextField[NBR_FIELDS];
		for (int i = 0; i < fields.length; i++) {
			fields[i] = new JTextField(20);
		}

		JPanel input = new InputPanel(texts, fields);

		JPanel p1 = new JPanel();
		p1.setLayout(new FlowLayout(FlowLayout.LEFT));

		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.add(p1);
		p.add(input);
		return p;
	}
	/**
	 * Creates the bottom panel.
	 */
	@Override
	public JComponent createBottomPanel() {
		JButton[] buttons = new JButton[1];
		buttons[0] = new JButton("Add record");
		return new ButtonAndMessagePanel(buttons, messageLabel,
				new ActionHandler());
	}

	class ActionHandler implements ActionListener {
		/**
		 * Called when the user clicks the add record button. Creates a record and
		 * send it to the server, which will store it in the database.
		 * 
		 * @param e
		 *            The event object (not used).
		 */
		public void actionPerformed(ActionEvent e) {

			if (CurrentUser.instance().isLoggedIn()) {
				if (noEmptyFields()) {
					String output = CurrentUser.instance().toString()
							+ ";append;" + fields[SSN].getText() + ":"
							+ fields[FULL_NAME].getText() + ":"
							+ fields[NURSE].getText() + ":"
							+ fields[DIVISION].getText() + ":"
							+ fields[SYMPTOMES].getText() + ":"
							+ fields[DIAGNOSIS].getText();
					if (cc.sendData(output).equalsIgnoreCase("ACK")) {
						displayMessage("Patient record was Succesfully added");
						clearFields();
					} else {
						displayMessage("Unable to add patient record");
					}
				} else {
					displayMessage("You must fill out all fields");
				}
			} else {
				displayMessage("You must be logged in");
			}

		}
		/**
		 * Controls that no information fields in the add record process are empty.
		 * @return
		 * 			true if no fields are empty.
		 */
		public boolean noEmptyFields() {
			for (int i = 0; i < NBR_FIELDS; i++) {
				if (fields[i].getText().equals("")) {
					return false;
				}
			}
			return true;
		}
		/**
		 * Clear all fields.
		 */
		private void clearFields() {
			for (int i = 0; i < fields.length; i++) {
				fields[i].setText("");
			}
		}
	}
}
